package com.yls.except.service.impl;

import com.yls.except.dao.*;
import com.yls.except.service.ShopService;
import com.yls.except.util.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 商家商品service
 * Created by Administrator on 2016/8/4.
 */
@Service
@Repository(value = "shopService")
public class ShopServiceImpl implements ShopService {
    @Resource(name = "shopDao")
    private ShopDao shopDao;
    @Resource(name = "imgDao")
    private ImgDao imgDao;
    @Resource(name = "systemDao")
    private SystemDao systemDao;
    @Resource(name = "userDao")
    private UserDao userDao;
    @Resource(name = "indentDao")
    private IndentDao indentDao;

    @Override
    public List<Object> selectPageHome(String page, String count, String type) {
        if (StringIsempty.isallempty(page))
            page = "0";
        if (StringIsempty.isallempty(count))
            count = "10";
        Map<String, Object> storymap = PageUtil.getNowPage(Integer.parseInt(page), Integer.parseInt(count));
        if("1".equals(type))
            storymap.put("storytype", " and type =" + type);
        else
            storymap.put("storytype", "");
        List<Map<String, Object>> storylistinfo = shopDao.selectStoryListInfo(storymap);
        List<Object> list = new ArrayList<Object>();
        getStoryInfo(type, storylistinfo, list);
        return list;
    }

    private void getStoryInfo(String type, List<Map<String, Object>> storylistinfo, List<Object> list) {
        if (!StringIsempty.isNullList(storylistinfo)) {
            for (int i = 0; i < storylistinfo.size(); i++) {
                Map<String, Object> storyinfo = storylistinfo.get(i);
                if (StringIsempty.isNullMap(storyinfo))
                    continue;
                getStoryType(storyinfo, shopDao);
                String storytype = storyinfo.get("type") + "";
                if("1".equals(type) && !"1".equals(storytype))
                    storylistinfo.set(i, null);
                else
                    storylistinfo.set(i, storyinfo);
            }
            list.addAll(storylistinfo);
        }
    }

    @Override
    public List<Object> selectHomePageBanner() {
        List<Map<String, Object>> banner = imgDao.selectImgByTyep("1");
        List<Object> list = new ArrayList<Object>();
        if (!StringIsempty.isNullList(banner))
            list.addAll(banner);
        return list;
    }

    @Override
    public Map<String, Object> selectStoryInfoById(String storyid, String uid) {
        Map<String, Object> storyinfo = shopDao.selectStoryInfoById(storyid);
        if (StringIsempty.isNullMap(storyinfo))
            return null;
        getStoryType(storyinfo, shopDao);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", 0);
        map.put("count", 3);
        map.put("storyid", storyid);
        List<Map<String, Object>> evaluate = shopDao.selectEvaluateByStoryId(map);
        if (!StringIsempty.isNullList(evaluate)) {
            getEvaluateInfo(evaluate, uid, shopDao);
            storyinfo.put("evaluate", evaluate);
        } else
            storyinfo.put("evaluate", new ArrayList<Object>());
        storyinfo.put("tag", 1);
        return storyinfo;
    }

    @Override
    public List<Object> selectEvaluate(String page, String count, String uid, String storyid) {
        int nowpage = 0;
        int pagesize = 10;
        if(!StringIsempty.isallempty(page))
            nowpage = Integer.parseInt(page);
        if(!StringIsempty.isallempty(count))
            pagesize = Integer.parseInt(count);
        Map<String, Object> map = PageUtil.getNowPage(nowpage, pagesize);
        map.put("storyid", storyid);
        List<Map<String, Object>> evaluate = shopDao.selectEvaluateByStoryId(map);
        List<Object> list = new ArrayList<Object>();
        if (!StringIsempty.isNullList(evaluate)) {
            getEvaluateInfo(evaluate, uid, shopDao);
            list.addAll(evaluate);
        }
        return list;
    }

    @Override
    public int insertStoryEvaluate(String uid, String storyid, String text, String reqip) {
        Map<String, Object> evaluate = new HashMap<String, Object>();
        evaluate.put("evaluate_id", ValueJude.getUuid());
        evaluate.put("story_id", storyid);
        evaluate.put("uid", uid);
        evaluate.put("evaluate_desc", text);
        evaluate.put("addtime", new Date().getTime());
        evaluate.put("addip", reqip);
        evaluate.put("status", 0);
        evaluate.put("isdisabled", 1);
        return shopDao.insertStoryEvaluate(evaluate);
    }

    @Override
    public int insertStoryEvaluatePraise(String uid, String evaluateid, String reqip) {
        Map<String, Object> evaluate = shopDao.selectEvaluateInfoById(evaluateid);
        if(StringIsempty.isNullMap(evaluate))
            return -13;
        String stroyid = evaluate.get("story_id") + "";
        Map<String, Object> praiseMap = new HashMap<String, Object>();
        praiseMap.put("uid", uid);
        praiseMap.put("evaluateid", evaluateid);
        Map<String, Object> praiseinfo = shopDao.selectEvaluatePraiseInfoByUidAndEvaluateId(praiseMap);
        int tag;
        if(StringIsempty.isNullMap(praiseinfo)) {
            praiseinfo = new HashMap<String, Object>();
            praiseinfo.put("evaluate_id", evaluateid);
            praiseinfo.put("uid", uid);
            praiseinfo.put("status", 1);
            praiseinfo.put("addtime", new Date().getTime());
            praiseinfo.put("addip", reqip);
            praiseinfo.put("isdisabled", 1);
            tag = shopDao.insertEvaluatePraise(praiseinfo);
        } else {
            if("1".equals(praiseinfo.get("status") + ""))
                return -61;
            praiseinfo.put("status", 1);
            praiseinfo.put("addtime", new Date().getTime());
            praiseinfo.put("addip", reqip);
            praiseinfo.put("isdisabled", 1);
            tag = shopDao.updateEvaluatePraise(praiseinfo);
        }
        if(tag <= 0)
            return 0;
        Map<String, Object> collect = new HashMap<String, Object>();
        collect.put("storyid", stroyid);
        collect.put("uid", uid);
        Map<String, Object> collectInfo = shopDao.selectStoryCollectByStoryIdAndUid(collect);
        if(StringIsempty.isNullMap(collectInfo)) {
            collectInfo = new HashMap<String, Object>();
            collectInfo.put("story_id", stroyid);
            collectInfo.put("uid", uid);
            collectInfo.put("status", 1);
            collectInfo.put("addtime", new Date().getTime());
            collectInfo.put("addip", reqip);
            collectInfo.put("isdisabled", 1);
            tag = shopDao.insertStoryCollect(collectInfo);
        } else {
            if("1".equals(collectInfo.get("status") + ""))
                tag = 1;
            else {
                collectInfo.put("status", 1);
                collectInfo.put("addtime", new Date().getTime());
                collectInfo.put("addip", reqip);
                collectInfo.put("isdisabled", 1);
                tag = shopDao.updateStoryCollect(collectInfo);
            }
        }
        return tag;
    }

    @Override
    public Map<String, Object> selectShopInfoById(String shopid, String uid) {
        Map<String, Object> shopinfo = shopDao.selectShopInfoById(shopid);
        if(StringIsempty.isNullMap(shopinfo))
            return null;
        List<Map<String, Object>> specprice = shopDao.selectShopSpecPriceByShopId(shopid);
        if(StringIsempty.isNullList(specprice))
            return null;
        getShopPriceInfo(specprice);
        shopinfo.put("specprice", specprice);
        List<Map<String, Object>> shopfavorabled = shopDao.selectShopFavorabledByShopId(shopid);
        if(StringIsempty.isNullList(shopfavorabled))
            shopinfo.put("shopfavorabled", new ArrayList<Object>());
        else {
            for (int i = 0; i < shopfavorabled.size(); i ++) {
                Map<String, Object> shopfavorabledinfo = shopfavorabled.get(i);
                if(StringIsempty.isNullMap(shopfavorabledinfo))
                    continue;
                shopfavorabledinfo.put("lose", ValueJude.getDecimaNum(shopfavorabledinfo.get("lose") + "", 1));
                shopfavorabled.set(i, shopfavorabledinfo);
            }
            shopinfo.put("shopfavorabled", shopfavorabled);
        }
        List<Map<String, Object>> badyage = systemDao.selectBadyAge();
        if(StringIsempty.isNullList(badyage))
            shopinfo.put("badyage", new ArrayList<Object>());
        else
            shopinfo.put("badyage", badyage);

        shopinfo.put("usertype", getUserType(uid, userDao));

        shopinfo.put("tag", 1);
        return shopinfo;
    }

    public static String getUserType(String uid, UserDao userDao) {
        if(StringIsempty.isallempty(uid))
            return "0";
        else {
            Map<String, Object> user = userDao.selectUserInfoByUid(uid);
            if(StringIsempty.isNullMap(user))
                return "0";
            else
                return user.get("type") + "";
        }
    }

    private void getShopPriceInfo(List<Map<String, Object>> specprice) {
        for (int i = 0; i < specprice.size(); i ++){
            Map<String, Object> map = specprice.get(i);
            if(StringIsempty.isNullMap(map))
                continue;
            map.put("price", ValueJude.getDecima(map.get("price") + ""));
            map.put("first", ValueJude.getDecima(map.get("first") + ""));
            map.put("second", ValueJude.getDecima(map.get("second") + ""));
            map.put("third", ValueJude.getDecima(map.get("third") + ""));
            map.put("forth", ValueJude.getDecima(map.get("forth") + ""));
            map.put("fifth", ValueJude.getDecima(map.get("fifth") + ""));
            specprice.set(i, map);
        }
    }

    @Override
    public int insertShopCart(String uid, String shopid, String num, String bady_sex, String bady_age_id, String shop_spec_price, String reqip) {
        Map<String, Object> shopcart = new HashMap<String, Object>();
        shopcart.put("uid", uid);
        shopcart.put("shop_id", shopid);
        shopcart.put("bady_sex", bady_sex);
        shopcart.put("bady_age_id", bady_age_id);
        shopcart.put("status", 1);
        shopcart.put("shop_spec_price", shop_spec_price);
        Map<String, Object> shopcartinfo = shopDao.selectShopCartInfoByUidAndShopIdAndSexAndAgeAndShopSpecPrice(shopcart);
        if(StringIsempty.isNullMap(shopcartinfo)) {
            shopcart.put("num", num);
            shopcart.put("shopcart_id", ValueJude.getUuid());
            shopcart.put("addtime", new Date().getTime());
            shopcart.put("addip", reqip);
            shopcart.put("isdisabled", 1);
            return shopDao.insertShopCart(shopcart);
        } else {
            shopcartinfo.put("num", Long.parseLong(shopcartinfo.get("num") + "") + Long.parseLong(num));
            return shopDao.updateShopCartInfo(shopcartinfo);
        }
    }

    @Override
    public Map<String, Object> selectShopCartByUid(String uid) {
        List<Map<String, Object>> shopcart = shopDao.selectShopCartByUid(uid);
        List<Object> list = new ArrayList<Object>();
        if(!StringIsempty.isNullList(shopcart)) {
//            for (int i = 0; i < shopcart.size(); i ++) {
//                Map<String, Object> shopcartinfo = shopcart.get(i);
//                if(StringIsempty.isNullMap(shopcartinfo))
//                    continue;
//                shopcartinfo.put("price", ValueJude.getDecima(shopcartinfo.get("price") + ""));
//                shopcart.set(i, shopcartinfo);
//            }
            getShopPriceInfo(shopcart);
            list.addAll(shopcart);
        }
        if(StringIsempty.isNullList(list))
            return null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("shopcart", list);
        map.put("usertype", getUserType(uid, userDao));
        map.put("tag", 1);
        return map;
    }

    @Override
    public int updateShopCart(String uid, String cartinfo) {
        Map<String, Object> map = GeneralResult.jsonStrToMap(cartinfo);
        if(StringIsempty.isNullMap(map))
            return -1;
        JSONArray cart = (JSONArray) map.get("cartinfo");
        for (int i = 0; i < cart.length(); i ++) {
            try {
                String num = cart.getJSONObject(i).get("num") + "";
                String shopcartid = cart.getJSONObject(i).get("shopcartid") + "";
                Map<String, Object> shopcartinfo = shopDao.selectShopCartInfoById(shopcartid);
                if(StringIsempty.isNullMap(shopcartinfo))
                    continue;
                if(!uid.equals(shopcartinfo.get("uid") + ""))
                    continue;
                shopcartinfo.put("num", num);
                shopDao.updateShopCartInfo(shopcartinfo);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return 1;
    }

    @Override
    public int deleteShopCart(String uid, JSONArray cartinid) {
        for (int i = 0; i < cartinid.length(); i ++) {
            try {
                String id = cartinid.get(i) + "";
                Map<String, Object> shopcartinfo = shopDao.selectShopCartInfoById(id);
                if(StringIsempty.isNullMap(shopcartinfo))
                    continue;
                if(!uid.equals(shopcartinfo.get("uid") + ""))
                    continue;
                shopDao.deleteShopCartInfo(id);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return 1;
    }

    @Override
    public List<Object> selectMyCollect(String uid, String page) {
        int nowpage = 0;
        if(!StringIsempty.isallempty(page))
            nowpage = Integer.parseInt(page);
        Map<String, Object> map = PageUtil.getNowPage(nowpage);
        map.put("uid", uid);
        List<Map<String, Object>> storyinfo = shopDao.selectCollectStoryByUid(map);
        List<Object> list = new ArrayList<Object>();
        getStoryInfo("", storyinfo, list);
        return list;
    }

    @Override
    public int deleteMyCollect(String uid, String story_id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("storyid", story_id);
        map.put("uid", uid);
        return shopDao.deleteMyCollect(map);
    }

    @Override
    public List<Object> selectIchibanBanner() {
        List<Map<String, Object>> banner = imgDao.selectImgByTyep("2");
        List<Object> list = new ArrayList<Object>();
        if (!StringIsempty.isNullList(banner))
            list.addAll(banner);
        return list;
    }

    @Override
    public Map<String, Object> selectShopListInfo(String page, String uid) {
        int nowpage = 0;
        if(!StringIsempty.isallempty(page))
            nowpage = Integer.parseInt(page);
        Map<String, Object> map = PageUtil.getNowPage(nowpage);
        List<Map<String, Object>> shoplist = shopDao.selectShopInfo(map);
        List<Object> list = new ArrayList<Object>();
        if(!StringIsempty.isNullList(shoplist)) {
            getShopPriceInfo(shoplist);
            list.addAll(shoplist);
        }

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("shopinfo", list);
        result.put("tag", 1);


        result.put("usertype", getUserType(uid, userDao));

        return result;
    }

    @Override
    public Map<String, Object> selectShopPriceListInfo() {
        List<Map<String, Object>> shoplist = shopDao.selectShopPriceList();
        List<Object> list = new ArrayList<Object>();
        if(!StringIsempty.isNullList(shoplist)) {
            getShopPriceInfo(shoplist);
            list.addAll(shoplist);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("tag", 1);
        return map;
    }

    public static void getStoryType(Map<String, Object> storyinfo, ShopDao shopDao) {
        String type = storyinfo.get("type") + "";
        if ("1".equals(type)) {
            String shopid = storyinfo.get("shopid") + "";
            Map<String, Object> shopinfo = shopDao.selectShopInfoById(shopid);
            if (StringIsempty.isNullMap(shopinfo))
                storyinfo.put("type", 0);
        }
    }

    public static void getEvaluateInfo(List<Map<String, Object>> evaluate, String uid, ShopDao shopDao) {
        for (int i = 0; i < evaluate.size(); i++) {
            Map<String, Object> eavluateinfo = evaluate.get(i);
            if (StringIsempty.isNullMap(eavluateinfo))
                continue;
            eavluateinfo.put("addtime", DateStringLong.longToString(Long.parseLong(eavluateinfo.get("addtime") + ""), "yyyy-MM-dd HH:mm:ss"));
            String evaluate_id = eavluateinfo.get("evaluate_id") + "";
            List<Map<String, Object>> reply = shopDao.selectEvaluateReplyByEvaluateId(evaluate_id);
            if(StringIsempty.isNullList(reply))
                eavluateinfo.put("reply", new ArrayList<Object>());
            else {
                for (int j = 0; j < reply.size(); j ++) {
                    Map<String, Object> replyinfo = reply.get(j);
                    if(StringIsempty.isNullMap(replyinfo))
                        continue;
                    replyinfo.put("addtime", DateStringLong.longToString(Long.parseLong(replyinfo.get("reply_time") + ""), "yyyy-MM-dd HH:mm:ss"));
                    reply.set(i, replyinfo);
                }
                eavluateinfo.put("reply", reply);
            }
            if (!StringIsempty.isallempty(uid)) {
                Map<String, Object> praise = new HashMap<String, Object>();
                praise.put("evaluate_id", evaluate_id);
                praise.put("uid", uid);
                int count = shopDao.selectEvaluatePraiseByEvaluateAndUid(praise);

                if (count > 0)
                    eavluateinfo.put("ispraise", true);
                else
                    eavluateinfo.put("ispraise", false);
            } else
                eavluateinfo.put("ispraise", false);
            int count = shopDao.selectEvaluatePraiseCountByEvaluateId(evaluate_id);
            eavluateinfo.put("praisecount", count);
            evaluate.set(i, eavluateinfo);
        }
    }
}
